<template>
  <view>
    <web-view :src="src" @message="handleViewMessage"></web-view>
  </view>
</template>
<script setup>
import { onShareAppMessage, onLoad } from "@dcloudio/uni-app";
import { reactive, ref } from "vue";
import { ObjectToQueryString, ossImg } from "@/utils/utils";
import { onMounted } from "vue";
import { config } from "./config";

const src = ref("");
const sceneObj = reactive({ scene: "", wx_scene: 0 })
const state = reactive({
  mp3id: null
})

const getQueryParams = (user = {}, exactParams = {}) => ({
  'accesstoken': uni.getStorageSync("TOKEN"),
  'openid': uni.getStorageSync("OPENID"),
  'unionid': uni.getStorageSync("UNIONID"),
  'memberid': user?.memberScore?.memberId || '',
  'agree': 'true',
  ...exactParams
})

onLoad(options => {
  getApp().accessLogAdd();
  sceneObj.scene = options?.scene || ""
  sceneObj.wx_scene = uni.getLaunchOptionsSync().scene
  if (options?.wall) sceneObj.wall = 1
  if (options?.mp3id) sceneObj.mp3id = options.mp3id
})

onMounted(async () => {
  try {
    await getApp().getUserLoginInfo()
    const user = await getApp().getUserInfo()
    const params = getQueryParams(user, sceneObj)
    src.value = `${config.hostname}?${ObjectToQueryString(params)}`
  } catch (e) {
    uni.showToast({ title: '服务器响应错误', icon: 'none' })
  }
})

const handleViewMessage = ({ detail }) => {
  console.log('--------------POSTMESSAGE-------------------')
  console.log(JSON.stringify(detail.data))
  console.log('--------------POSTMESSAGE-------------------')
  state.mp3id = detail.data?.at(-1)?.id || ''
}


onShareAppMessage(() => {
  return {
    title: state.mp3id ? '这把坦白局，听听TA的真心话' : '奥利奥拍了拍你，参与赢大奖',
    imageUrl: ossImg(state.mp3id ? 'others/2024panda/video_share.jpg' : 'others/2024panda/panda_share.jpg'),
    path: `/campaign/pages/panda2024/index?scene=${state.mp3id ? 'KJLV1ZB7S8EV4VV41NA2W3UNBV' : 'KJTW0Z22ZTUZUXTIKZCR7FR4V0'}&mp3id=${state.mp3id || ''}`
  }
})
</script>

<style></style>
